﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace MuzičkiFeršterker
{
    public static class XmlSerializator
    {
        public static T Load<T>(string file) where T : class
        {
            T data;
            try
            {
                using (Stream stream = File.OpenRead(file))
                {
                    Type[] extraTypes = new Type[] { typeof(MuzičkiFeršterker.Translations.TransFile) };
                    XmlSerializer serializer = new XmlSerializer(typeof(T), extraTypes);
                    data = (T)serializer.Deserialize(stream);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in parsing");
                return null;
            }

            return data;
        }

        public static void Save<T>(string file, T data)
        {
            if (File.Exists(file))
                File.Delete(file);

            using (Stream stream = File.OpenWrite(file))
            {
                Type[] extraTypes = new Type[] { typeof(MuzičkiFeršterker.Translations.TransFile) };
                XmlSerializer serializer = new XmlSerializer(typeof(T), extraTypes);
                serializer.Serialize(stream, data);
            }
        }
    }
}
